package com.sve.Controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sve.Dao.UserMapper;
import com.sve.Entity.User;

@Controller
@RequestMapping("/user")
public class UserController {
	private UserMapper usermapper;

	public UserMapper getUsermapper() {
		return usermapper;
	}
	@Autowired
	public void setUsermapper(UserMapper usermapper) {
		this.usermapper = usermapper;
	}
	
	@RequestMapping("/Login")
	public String Loging()
	{
		return "Login";
	}
	
	@RequestMapping("/Res")
	public String Res()
	{
		return "Register";
	}
	@RequestMapping("/Orders")
	public String Orders()
	{
		return "Orders";
	}
	
	@RequestMapping(value="LoginAction",method=RequestMethod.POST)
	public String Login(HttpServletRequest request,HttpServletResponse response,Model model,HttpSession session) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String name=request.getParameter("txtUserCode");
		String pass=request.getParameter("txtPassword");
		String validate=request.getParameter("txbValidate");
		String hid=request.getParameter("hid");
		List<User> ls=usermapper.SUserAccountNumberId(); 
		//System.out.println("jj"+hid+"  "+validate);
		if(name.trim().equals("")||pass.trim().equals("")||validate.trim().equals("")||((!validate.trim().equals(hid.toLowerCase())&&!validate.trim().equals(hid))))
		{
			return "Login";
		}
		else
		{
			for(User us:ls)
			{
				if(name.trim().equals(us.getUsername())&&pass.trim().equals(us.getUserpass()))
				{
					session.setAttribute("id", us.getUserid());
					session.setAttribute("name",us.getUsername());
					session.setAttribute("pass", us.getUserpass());
					session.setAttribute("email", us.getUseremail());
					session.setAttribute("phone", us.getUserphone());
					session.setAttribute("idcard", us.getUseridcard());
					session.setAttribute("realname",us.getUserrealname());
					
					return "redirect:/Station/index";
				}
			}
			
		}
		out.print("<script>alert('用户名或密码输入有误');window.location.href='Login'</script>");
		out.flush();
		out.close();
		return "Login";
	}
	@RequestMapping(value="registerAction",method=RequestMethod.POST)
	public String Register(HttpServletRequest request,HttpServletResponse response,Model model) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String UserCode=request.getParameter("txtUserCode");
		String Password=request.getParameter("txtPassword");
		String UserName =request.getParameter("txtUserName");
		String CardId =request.getParameter("txtCardId");
		String Email =request.getParameter("txtEmail");
		String Mobile =request.getParameter("txtMobile");
		String Validate =request.getParameter("txbValidate");
		String hid =request.getParameter("hid");
		String val1 =request.getParameter("val1");
		/*User u=null;
		try
		{
			u=usermapper.selectByPrimaryKey(UserCode);
		}catch(Exception e){}*/
		
		/*if(u.getUsername().trim().equals(""))
		{
			return "Login";
		}*/
		//System.out.println(u.getUserPass());
		
		List<User> ls=usermapper.SUserAccountNumberId();
		for(User ss:ls)
		{
			if(UserCode.trim().equals(ss.getUsername()))
			{
				out.print("<script>alert('用户名已被占用');window.location.href='Res'</script>");
				out.flush();
				out.close();
				return "Register";
			}
		}
		if(UserCode.trim().equals("")||Password.trim().equals("")||UserName.trim().equals("")||CardId.trim().equals("")||Email.trim().equals("")||Password.trim().equals("")||Mobile.trim().equals("")||Validate.trim().equals("")||!Validate.trim().equals(hid.toLowerCase())&&!Validate.trim().equals(hid)||Integer.parseInt(val1)!=0)
		{
			return "Register";
		}
		else
		{
			User u=new User();
			u.setUsername(UserCode);
			u.setUserpass(Password);
			u.setUserrealname(UserName);
			u.setUseridcard(CardId);
			u.setUseremail(Email);
			u.setUserphone(Mobile);
			int i=usermapper.insert(u);
			if(i==1)
			{
				out.print("<script>alert('注册成功');window.location.href='Login'</script>");
				out.flush();
				out.close();
				return "Login";
			}
			else
			{
				out.print("<script>alert('注册失败');window.location.href='Res'</script>");
				out.flush();
				out.close();
				return "Register";
			}
		}
		//System.out.println("fd"+UserCode+" "+Password+" "+Validate+" "+hid+" "+val1);
	}
	@RequestMapping(value="infoAction",method=RequestMethod.POST)
	public String info(HttpServletRequest request,HttpServletResponse response,Model model,HttpSession session) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String UserName =request.getParameter("txtUserName");
		String CardId =request.getParameter("txtCardId");
		String Email =request.getParameter("txtEmail");
		String Mobile =request.getParameter("txtMobile");
		String val1 =request.getParameter("val1");
		System.out.println("val1:"+val1);
		if(UserName.trim().equals("")||CardId.trim().equals("")||Email.trim().equals("")||Mobile.trim().equals("")||Integer.parseInt(val1)!=0)
		{
			return "Orders";
		}
		else
		{
			String name=(String) session.getAttribute("name");
			String pass=(String) session.getAttribute("pass");
			//System.out.println("ֵ��"+name);
			User u=new User();
			u.setUsername(name);
			u.setUserpass(pass);
			u.setUseremail(Email);
			u.setUserphone(Mobile);
			u.setUserrealname(UserName);
			u.setUseridcard(CardId);
			int i=usermapper.updateByPrimaryKey(u);
			if(i==1)
			{
				out.print("<script>alert('修改成功');window.location.href='Orders'</script>");
				out.flush();
				out.close();
				return "Orders";
			}
			else
			{
				out.print("<script>alert('修改失败');window.location.href='Orders'</script>");
				out.flush();
				out.close();
				return "Orders";
			}
		}
	}
	@RequestMapping(value="passAction",method=RequestMethod.POST)
	public String pass(HttpServletRequest request,HttpServletResponse response,Model model,HttpSession session) throws IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out=response.getWriter();
		String oldpass =request.getParameter("txtPassword1");
		String newpass =request.getParameter("txtPassword");
		String repass =request.getParameter("txtRePassword");
		String val1 =request.getParameter("val1");
		//System.out.println("fd:"+val1+" "+oldpass);
		String name=(String) session.getAttribute("name");
		User ls=usermapper.selectByPrimaryKey(name);
		//System.out.println("name��"+name);
		if(oldpass.trim().equals("")||newpass.trim().equals("")||repass.trim().equals(""))
		{
			return "Orders";
		}
		else if(!ls.getUserpass().equals(oldpass))
		{
			out.print("<script>alert('原密码输入有误');window.location.href='Orders'</script>");
			out.flush();
			out.close();
			return "Orders";
		}
		else
		{
			User u=new User();
			u.setUserpass(newpass);
			u.setUsername(name);
			int i=usermapper.updatePass(u);
			if(i==1)
			{
				out.print("<script>alert('密码修改成功');window.location.href='Orders'</script>");
				out.flush();
				out.close();
				return "Orders";
			}
			else
			{
				out.print("<script>alert('密码修改失败');window.location.href='Orders'</script>");
				out.flush();
				out.close();
				return "Orders";
			}
			
		}
	}
}
